package com.wang.luntan.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 线程池的工具类
 *
 * @author wangshMac
 */
public class ExecutorServiceUtil {
    /* 固定线程数的判断类型 */
    public static final String fiexNum = "fiexNum";

    /* 创建一个缓存的线程池
     * 线程池启动以后不用关闭
     *  */
    private static ExecutorService cacheThreadPool = Executors.newCachedThreadPool();
    /* 创建固定的线程数 */
    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(ConstatFinalUtil.CONFIG_JSON.getIntValue("thread.fixNum"));

    /**
     * 提交,主线程不用等待
     */
    public static List<Object> submit(List<Callable<Object>> callList) {
        List<Object> resultList = new ArrayList<Object>();
        for (Iterator iterator = callList.iterator(); iterator.hasNext(); ) {
            Callable<Object> call = (Callable<Object>) iterator.next();
            Object resTemp = cacheThreadPool.submit(call);
            resultList.add(resTemp);
        }
        return resultList;
    }

    /**
     * 提交,主线程不用等待
     */
    public static Object submit(Callable<Object> call) {
        return cacheThreadPool.submit(call);
    }

    /**
     * 主线程得等待
     *
     * @throws InterruptedException
     */
    public static List<Future<Object>> invokeAll(List<Callable<Object>> callList) throws InterruptedException {
        if (callList.size() > 0) {
            return cacheThreadPool.invokeAll(callList);
        }
        return Collections.emptyList();
    }

    /**
     * 选择一个合适的ExecutorService
     *
     * @return
     */
    public static ExecutorService selectExecutor(String operType) {
        /* 判断是否是固定线程 */
        if (fiexNum.equalsIgnoreCase(operType)) {
            /* 固定线程数 */
            return fixedThreadPool;
        }
        return cacheThreadPool;
    }

    /**
     * 提交,主线程不用等待
     */
    public static List<Object> submit(List<Callable<Object>> callList, String operType) {
        /* 选择一个线程池 */
        ExecutorService exectorTarget = selectExecutor(operType);

        List<Object> resultList = new ArrayList<Object>();
        for (Iterator iterator = callList.iterator(); iterator.hasNext(); ) {
            Callable<Object> call = (Callable<Object>) iterator.next();
            Object resTemp = exectorTarget.submit(call);
            resultList.add(resTemp);
        }
        return resultList;
    }

    /**
     * 提交,主线程不用等待
     */
    public static Object submit(Callable<Object> call, String operType) {
        /* 选择一个线程池 */
        ExecutorService exectorTarget = selectExecutor(operType);
        return exectorTarget.submit(call);
    }

    /**
     * 主线程得等待
     *
     * @throws InterruptedException
     */
    public static List<Future<Object>> invokeAll(List<Callable<Object>> callList, String operType) throws InterruptedException {
        /* 选择一个线程池 */
        ExecutorService exectorTarget = selectExecutor(operType);

        if (callList.size() > 0) {
            return exectorTarget.invokeAll(callList);
        }
        return Collections.emptyList();
    }

    /**
     * 关闭线程池服务
     */
    public static void shutdown() {
        cacheThreadPool.shutdown();
        /* 一旦抛异常,重新开启一个新的线程池 */
        cacheThreadPool = Executors.newCachedThreadPool();

        fixedThreadPool.shutdown();
        cacheThreadPool = Executors.newFixedThreadPool(ConstatFinalUtil.CONFIG_JSON.getIntValue("thread.fixNum"));
    }
}
